<?php
    include "Class/HttpResponse.php";
/**
 * Created by PhpStorm.
 * User: fmisx
 * Date: 7/13/2017
 * Time: 9:56 AM
 */
    class EasyMySQL
    {
        private $Host = "";
        private $Username = "";
        private $Password = "";
        private $Database = "";
        private $Query = "";
        private $Conn;
        private $Unit = 50;

        public function __construct()
        {
            $this->Host = "localhost";
            $this->Username = "root";
            $this->Password = "19b6518+";
            $this->Database = "Fruit";
        }

        public function Connect()
        {
            $this->Conn = mysqli_connect($this->Host, $this->Username, $this->Password, $this->Database);

            if(!$this->Conn)
            {
                $response = new HttpResponse();
                $response->S = "1";
                $response->D = "数据库连接失败！";

                ECHO json_encode($response,JSON_UNESCAPED_UNICODE);
                return false;
            }

            $this->Conn->query("SET NAMES 'utf8'");
            return true;
        }

        public function QuerySelect()
        {
            $response = new HttpResponse();

            //ECHO $this->Query;
            if($result = $this->Conn->query($this->Query))
            {
                $list = array();
                while($row = mysqli_fetch_array($result))
                {
                    $count=count($row);
                    for($i=0; $i<$count; $i++){
                        unset($row[$i]);
                    }
                    array_push($list, $row);
                }

                $count=count($list);
                if($count > 0)
                {
                    $data = json_encode($list, JSON_UNESCAPED_UNICODE);
                    $response->S = "0";
                    $response->D = $data;
                }
                else
                {
                    $response->S = "2";
                    $response->D = "";
                }
            }
            else
            {
                $response->S = "1";
                $response->D = mysqli_connect_error();
            }

            ECHO json_encode($response,JSON_UNESCAPED_UNICODE);
        }

        public function QueryCount()
        {
            $response = new HttpResponse();

            if($result = $this->Conn->query($this->Query))
            {
                $row = mysqli_fetch_array($result);
                $response->S = "0";
                $response->D = ceil($row["TOTAL"] / $this->Unit);
            }
            else
            {
                $response->S = "1";
                $response->D = mysqli_connect_error();
            }

            ECHO json_encode($response,JSON_UNESCAPED_UNICODE);
        }

        public function QueryInsert()
        {
            $response = new HttpResponse();

            if($result = $this->Conn->query($this->Query)) {
                $response->S = "0";
                $response->D = mysqli_insert_id($this->Conn);
            }
            else
            {
                $response->S = "1";
                $response->D = mysqli_connect_error();
            }

            ECHO json_encode($response,JSON_UNESCAPED_UNICODE);
        }

        public function Query()
        {
            $response = new HttpResponse();

            if($result = $this->Conn->query($this->Query)) {
                $response->S = "0";
                $response->D = "";
            }
            else
            {
                $response->S = "1";
                $response->D = mysqli_connect_error();
            }

            ECHO json_encode($response,JSON_UNESCAPED_UNICODE);
        }

        public function Close()
        {
            $this->Conn->close();
        }

        public function SetUnit($unit)
        {
            $this->Unit = $unit;
        }

        public function Select($Table, $Names="*")
        {
            $this->Query = "SELECT ".$Names." FROM ".$Table;

            return $this;
        }

        public function SelectCount($Table)
        {
            $this->Query = "SELECT COUNT(*) AS TOTAL FROM ".$Table;

            return $this;
        }

        public function Delete($Table)
        {
            $this->Query = "DELETE FROM ".$Table;

            return $this;
        }

        public function Insert($Table)
        {
            $this->Query = "INSERT INTO ".$Table;

            return $this;
        }

        public function Update($Table)
        {
            $this->Query = "UPDATE ".$Table;

            return $this;
        }

        //$query:key=value
        public function UpdateSet($query)
        {
            $this->Query = $this->Query." SET ".$query;

            return $this;
        }

        //$Query:Column1,Column2,Column3
        public function InsertColumns($Query)
        {
            $this->Query = $this->Query." (".$Query.")";

            return $this;
        }

        //$Query:Value1-Value2-Value3-Value1-Value2-Value3-Value1-Value2-Value3
        public function InsertValues($Query, $Count)
        {
            $values = explode("-", $Query);

            for ($index=1,$i=0; $i<count($values); $index++,$i++)
            {
                if($index == 1 && $i == 0)
                {
                    $this->Query = $this->Query." VALUES (";
                }
                else if($index == 1)
                {
                    $this->Query = $this->Query.",(";
                }

                $this->Query = $this->Query.$values[$i];
                if($index != $Count)
                {
                    $this->Query = $this->Query.",";
                }
                else
                {
                    $this->Query = $this->Query.")";
                    $index = 0;
                }
            }

            return $this;
        }

        //$Query:Key1=Value1&Key2=Value2&Key3=Value3
        public function WhereSelect($Query)
        {
            if($Query == null || $Query == "") return $this;

            $array = $this->StrToArray($Query);
            $count = count($array);
            if($count<=0) { return $this; }

            $index = 0;
            $this->Query = $this->Query." WHERE ";
            foreach($array as $key => $value)
            {
                $index++;
                if($index == $count)
                {
                    $and = "";
                }
                else
                {
                    $and = " AND ";
                }

                $this->Query = $this->Query.$key."='".$value."'".$and;
            }

            return $this;
        }

        //$Query:Value1,Value2,Value3
        public function WhereDelete($Key, $Query)
        {
            $this->Query = $this->Query." WHERE ".$Key." IN (".$Query.")";

            return $this;
        }

        //$Query:Value1,Value2,Value3
        public function WhereUpdate($Query)
        {
            $this->Query = $this->Query." WHERE ".$Query;

            return $this;
        }

        public function Limit($Page)
        {
            if($Page>0)
            {
                $this->Query = $this->Query." LIMIT ".($Page - 1)*$this->Unit.",".$Page*$this->Unit;
            }

            return $this;
        }

        //$Key: Table1.Key=Table2.Key
        public function LeftJoin($Table, $Key)
        {
            $this->Query = $this->Query." LEFT JOIN ".$Table." ON ".$Key;

            return $this;
        }

        //$Key: Table1.Key=Table2.Key
        public function InnerJoin($Table, $Key)
        {
            $this->Query = $this->Query." INNER JOIN ".$Table." ON ".$Key;

            return $this;
        }

        public function GroupBy($Key)
        {
            $this->Query = $this->Query." GROUP BY ".$Key;

            return $this;
        }

        public function StrToArray($str, $sp="&", $kv="=")
        {
            $arr = str_replace(array($kv,$sp), array('"=>"','","'),'array("'.$str.'")');
            eval("\$arr"." = $arr;");
            return $arr;
        }
    }
?>
